<?php

namespace app\admin\controller;

use app\BaseController;
use think\exception\ValidateException;
use think\facade\View;
use app\admin\common\business\admin\AdminUser as AdminUserBusiness;
use app\admin\common\validate\admin\AdminUser as AdminUserValidate;

class AdminUser extends BaseController
{
    public function addAdminUser()
    {
        $data = $this->request->param(["username", "password"]);
//        halt($data); // 输出参数 halt:输出变量,和echo区别：halt会终止程序运行

        try {
            $validate = validate(AdminUserValidate::class) -> check($data);
        } catch (ValidateException $e) {
            $judge = $e -> getMessage();
            // 返回错误信息
            return $this -> show(
                config("status.error"),
                config("status.error"),
                $judge
            );
        }
        $AdminUserBusiness = new AdminUserBusiness($this->app);
        $data =  $AdminUserBusiness -> passwordAddSalt($data);
        try {
            $AdminUserBusiness -> add($data);
        } catch (\Exception $e){
            return $this -> show(
                config("status.error"),
                config("status.error"),
                "插入失败"
            );
        }
        return $this ->show(
            config("status.success"),
            config("status.success"),
            "插入成功"
        );

    }
}